package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SettingsManager {

    @Reference
    private NotificationService notificationService; //通知的service

    @Reference
    private QuestionService questionService; //问题的service

    public SettingVo findMySettings(User user) {
        //1.查询通知
        Notification notification = notificationService.findByUserId(user.getId());
        if(notification == null){
            notification = new Notification();//给默认值
            notification.setLikeNotification(false); //不开启
            notification.setPinglunNotification(false);//不开启
            notification.setGonggaoNotification(false);//不开启
        }
        //2.查询问题
        Question question = questionService.findByUserId(user.getId());
        if(question == null ){
            question = new  Question();
            question.setStrangerQuestion("你喜欢高山还是流水?");//默认问题
        }
        //3.查询手机号
        //String phone = user.getPhone();
        //4.封装结果对象
        SettingVo vo = new SettingVo();


        //赋值
        BeanUtil.copyProperties(notification , vo );
        BeanUtil.copyProperties(question , vo );
        BeanUtil.copyProperties(user , vo );

        System.out.println(vo);
        return vo;
    }

    /**
     * 保存或者更新用户问题
     * @param content
     */
    public void saveOrUpdateQuestions(String content) {
        //获得对象
        //1.先查看用户有没有这个问题
        Question question = questionService.findByUserId(UserHolder.get().getId());
        if(question==null){
            //3.如果没有添加
            question = new Question(); //创建对象
            question.setStrangerQuestion(content);//设置问题
            question.setUserId( UserHolder.get().getId() );
            questionService.save(question);
        }else{
            //2.如果有修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }

    //保存或者更新通知
    public void saveOrUpdateNotifications(Notification notification) {
        //这个id是userId
        Long userId = UserHolder.get().getId();
        //1.先查看用户有没有这个通知
        Notification tempNotification = notificationService.findByUserId(userId);
        if(tempNotification == null){
            //3.如果没有添加
            notification.setUserId( userId );//赋值用户的id数据
            notificationService.save(notification);
        }else{

            notification.setId(tempNotification.getId()  );
            //2.如果有修改  只改三个通知字段
            notificationService.update(notification);

            //notificationService.update(浏览器传入的对象 缺少id);
            //notificationService.update(数据库查询的对象 三个通知);
        }


    }


    @Reference
    private BlackListService blackListService;
    /**
     * 黑名单用户查询
     * @param pageNumber  页码
     * @param pageSize  每页展示的个数
     * @return
     */
    public PageBeanVo findBlackList(Integer pageNumber, Integer pageSize) {
        //1.获得用户的id
        Long userId = UserHolder.get().getId();
        //2.调用分页进行查询
        return blackListService.findBlackUserByUserId(pageNumber , pageSize , userId);
    }

    /**
     * 删除黑名单用户
     * @param blackUserId 黑名单用户的id
     * @return
     */
    public Map deleteBlackUser(Long blackUserId) {
        //获得当前用户id
        Long userId = UserHolder.get().getId();
        //删除用户
        blackListService.deleteBlackUser(userId , blackUserId);
        return null;
    }
}
